package com.lzx.adx.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;

import com.lzx.adx.report.cache.OcpxAccountLocalCache;
import com.lzx.adx.report.dao.OcpxAccountDao;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.entity.OcpxAccountEntity;
import com.lzx.adx.report.service.OcpxAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2024-10-03
 */
@Service
public class OcpxAccountServiceImpl extends CrudServiceImpl<OcpxAccountDao, OcpxAccountEntity, OcpxAccountDTO> implements OcpxAccountService {

    @Override
    public QueryWrapper<OcpxAccountEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String name = (String)params.get("name");
        String business = (String)params.get("business");
        String productChannel = (String)params.get("productChannel");
        String mediaChannel = (String)params.get("mediaChannel");

        QueryWrapper<OcpxAccountEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(name), "name", name);
        wrapper.eq(StringUtils.isNotBlank(business), "business", business);
        wrapper.eq(StringUtils.isNotBlank(productChannel), "product_channel", productChannel);
        wrapper.eq(StringUtils.isNotBlank(mediaChannel), "media_channel", mediaChannel);

        wrapper.orderByDesc("update_time");
        return wrapper;
    }


    @Override
    public OcpxAccountEntity getDetail(String id) {


        return  this.baseDao.selectById(id);
    }

    @Override
    public void deleteBatch(String[] ids) {

        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public void sysnCacheToDb() {
        List<OcpxAccountEntity> ocpxAccountEntities = this.baseDao.selectList(new QueryWrapper<>());
        for (OcpxAccountEntity ocpxAccountEntity : ocpxAccountEntities) {
            OcpxAccountDTO ocpxAccountDTO=new OcpxAccountDTO();
            BeanUtils.copyProperties(ocpxAccountEntity,ocpxAccountDTO);
            OcpxAccountLocalCache.getInstance().put(ocpxAccountEntity.getId(),ocpxAccountDTO);
        }
    }


}